
// TechniqueVerificationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TechniqueVerification.h"
#include "TechniqueVerificationDlg.h"
#include "afxdialogex.h"
#include "JGraphRowColGenerator.h"
#include "JGraphOrigin.h"
#include "JPaneStage.h"
#include "JPaneStageDCRender.h"
#include "DivisonSettingRowColDlg.h"
#include "DisplaySettingDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTechniqueVerificationDlg dialog



CTechniqueVerificationDlg::CTechniqueVerificationDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CTechniqueVerificationDlg::IDD, pParent)
{
	m_curDoc.pCurGraphOrigin = NULL;
	m_curDoc.strImagePath = _T("E:\\My Codes\\Jigsaw puzzle\\src\\TP\\Debug\\images\\b.jpg");
	m_curDoc.pPaneStage = new JPaneStage;
	m_curDoc.pRender = new JPaneStageDCRender(&m_curDoc, &m_wndBoardView);
	m_wndBoardView.SetRender((JPaneStageDCRender*)m_curDoc.pRender);

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTechniqueVerificationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_BOARD, m_wndBoardView);
}

BEGIN_MESSAGE_MAP(CTechniqueVerificationDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_FILE_GENERATE, &CTechniqueVerificationDlg::OnFileGenerate)
	ON_COMMAND(ID_FILE_LOAD, &CTechniqueVerificationDlg::OnFileLoad)
	ON_COMMAND(ID_FILE_START, &CTechniqueVerificationDlg::OnFileStart)
	ON_COMMAND(ID_GAME_EXIT, &CTechniqueVerificationDlg::OnGameExit)
	ON_COMMAND(ID_OPTION_DIVISIONSETTING, &CTechniqueVerificationDlg::OnOptionDivisionsetting)
	ON_COMMAND(ID_OPTION_DISPLAYSETTING, &CTechniqueVerificationDlg::OnOptionDisplaysetting)
END_MESSAGE_MAP()


// CTechniqueVerificationDlg message handlers

BOOL CTechniqueVerificationDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	OnImageChanged();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTechniqueVerificationDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTechniqueVerificationDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTechniqueVerificationDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CTechniqueVerificationDlg::OnFileGenerate()
{
	if (m_curDoc.pPaneStage)
	{
		m_curDoc.pPaneStage->Stop();
	}
	if (m_curDoc.pCurGraphOrigin)
	{
		delete m_curDoc.pCurGraphOrigin;
		m_curDoc.pCurGraphOrigin = NULL;
	}
	if (!m_curDoc.curImage.IsNull())
	{
		JGraphRowColGenerator generator;
		CRect rc(0, 0, 
			m_curDoc.curImage.GetWidth(),
			m_curDoc.curImage.GetHeight());

		m_curDoc.pCurGraphOrigin = generator.CreateGraph(rc);

		m_wndBoardView.Invalidate();
	}
}


void CTechniqueVerificationDlg::OnFileLoad()
{
	CFileDialog dlg(TRUE, _T("All Supported Format(*.bmp, *.jpg, *.png)|*.bmp;*.jpg;*.png"),
		_T(""),
		OFN_HIDEREADONLY,
		_T("All Supported Format(*.bmp, *.jpg, *.png)|*.bmp;*.jpg;*.png|Bitmaps (*.bmp)|*.bmp|JPEG Image File (*.jpg;*.jpeg)|*.jpg;*.jpeg|Portable Network Graphics (*.png)|*.png|GIF Image (*.gif)|*.gif||"),
		AfxGetMainWnd());

	if (dlg.DoModal() == IDOK)
	{
		m_curDoc.strImagePath = dlg.GetPathName();
		OnImageChanged();
		
		m_wndBoardView.Invalidate();
	}
}

CTechniqueVerificationDlg::~CTechniqueVerificationDlg()
{

}


void CTechniqueVerificationDlg::OnFileStart()
{
	if (m_curDoc.pCurGraphOrigin)
	{
		CRect rc;
		m_wndBoardView.GetClientRect(rc);
		if (!m_curDoc.curImage.IsNull() && 
			m_curDoc.pCurGraphOrigin &&
			m_curDoc.pPaneStage)
		{
			m_curDoc.pPaneStage->Stop();
			m_curDoc.pPaneStage->Start(&m_curDoc, rc);
		}

		m_wndBoardView.Invalidate();
	}
}

void CTechniqueVerificationDlg::OnImageChanged()
{
	if (!m_curDoc.curImage.IsNull())
	{
		m_curDoc.curImage.Destroy();
	}
	m_curDoc.curImage.Load(m_curDoc.strImagePath);

	if (m_curDoc.pCurGraphOrigin)
	{
		delete m_curDoc.pCurGraphOrigin;
		m_curDoc.pCurGraphOrigin = NULL;
	}

	CRect rc;
	m_wndBoardView.GetClientRect(rc);
	m_curDoc.pPaneStage->OnImageLoaded(&m_curDoc, rc);
}


void CTechniqueVerificationDlg::OnGameExit()
{
	OnCancel();
}


void CTechniqueVerificationDlg::OnOptionDivisionsetting()
{
	CDivisonSettingRowColDlg dlg;
	dlg.m_nRow = JGraphRowColGenerator::m_nRowNum;
	dlg.m_nCol = JGraphRowColGenerator::m_nColNum;
	if (dlg.DoModal() == IDOK)
	{
		JGraphRowColGenerator::m_nRowNum = dlg.m_nRow;
		JGraphRowColGenerator::m_nColNum = dlg.m_nCol;
	}
}


void CTechniqueVerificationDlg::OnOptionDisplaysetting()
{
	CDisplaySettingDlg dlg;
	dlg.m_clrBK = JPaneStageDCRender::g_clrBK;
	dlg.m_clrCurrent = JPaneStageDCRender::g_clrCurrentBorder;
	dlg.m_clrNormal = JPaneStageDCRender::g_clrNormalBorder;
	dlg.m_clrHot = JPaneStageDCRender::g_clrHotBorder;
	dlg.m_bShowPrompt = JPaneStageDCRender::g_showPrompt;
	if (dlg.DoModal() == IDOK)
	{
		JPaneStageDCRender::g_clrBK = dlg.m_clrBK;
		JPaneStageDCRender::g_clrCurrentBorder = dlg.m_clrCurrent;
		JPaneStageDCRender::g_clrNormalBorder = dlg.m_clrNormal;
		JPaneStageDCRender::g_clrHotBorder = dlg.m_clrHot;
		JPaneStageDCRender::g_showPrompt = dlg.m_bShowPrompt ? true : false;
		m_wndBoardView.Invalidate();
	}
}
